﻿

namespace Mulala.Cavan.Presentation.Mvc.Controller
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web.Mvc;
    using Mulala.Cavan.Application.MainBoundedContext.AttachmentModule;
    using Mulala.Cavan.Application.MainBoundedContext.ConsignModule;
    using Mulala.Cavan.Application.MainBoundedContext.InformationModule;
    using Mulala.Cavan.Application.MainBoundedContext.NannyModule;
    using Mulala.Cavan.Core.Extension;
    using Mulala.Cavan.Core.Settings;
    using Mulala.Cavan.Domain.MainBoundedContext.AttachmentModule;
    using Mulala.Cavan.Domain.MainBoundedContext.ConsignModule;
    using Mulala.Cavan.Domain.MainBoundedContext.InformationModule;
    using Mulala.Cavan.Domain.MainBoundedContext.NannyModule;
    using Mulala.Cavan.Domain.MainBoundedContext.SearchModule;
    using Mulala.Cavan.Domain.MainBoundedContext.SecurityModule;
    using Mulala.Cavan.Domain.Seedwork;
    using Mulala.Cavan.Presentation.Mvc.ViewModel;
    using Mulala.Cavan.Presentation.Seedwork;
    using Mulala.Cavan.Domain.MainBoundedContext.Setting;

    public class NannyCenterController : EntityController<Nanny, NannySearch, INannyViewModel, INannyAppService>, INannyCenterController
    {
        private IAttachmentAppService AttachmentAppService { get; set; }
        private IConsignAppService ConsignAppService { get; set; }
        private IFileUploadAppService FileUploadAppService { get; set; }
        private IUserMessageAppService UserMessageAppService { get; set; }
        private INannyDiaryAppService NannyDiaryAppService { get; set; }

        public NannyCenterController()
        {
            this.Model = new NannyViewModel();
        }

        public override string ControllerName
        {
            get { return ControllerConst.NANNYCENTER; }
        }

        public override bool UseDenied
        {
            get { return true; }
        }

        [ActionName("index")]
        public ActionResult GetUserCenterInfo()
        {
            Nanny nanny = this.AppService.GetById(this.Model.MasterModel.CurrentUserModel.UserAccountID);
            NannyViewModelConverter.Convert(nanny, this.Model);

            ConsignBidderSearch consignBidderSearch = ConsignBidderSearchFactory.CreateConsignBidderSearch(this.Model.MasterModel.CurrentUserModel.UserAccountID, null);
            this.Model.BiddedConsignCount = this.ConsignAppService.GetConsignBidderCount(consignBidderSearch);

            consignBidderSearch = ConsignBidderSearchFactory.CreateConsignBidderSearch(this.Model.MasterModel.CurrentUserModel.UserAccountID, null, ConsignIdentifier.ConsignBidderStatus.IsAccepted);
            this.Model.BiddedSuccessConsignCount = this.ConsignAppService.GetConsignBidderCount(consignBidderSearch);

            return View(this.Model);
        }

        [ActionName(ActionConst.PARTINFO)]
        [ChildActionOnly]
        public ActionResult GetNannyPartInfo()
        {
            Nanny nanny = this.AppService.GetById(this.Model.MasterModel.CurrentUserModel.UserAccountID);
            NannyViewModelConverter.Convert(nanny, this.Model);

            return PartialView("~/Views/NannyCenter/_nannyAccessPartial.cshtml", this.Model);
        }

        [HttpGet]
        [ActionName(ActionConst.INFO)]
        public ActionResult GetNannyInfo()
        {
            INannyViewModel nannyViewModel = ModelFactory.CreateModel<INannyViewModel>();
            Nanny nanny = this.AppService.GetById(this.Model.MasterModel.CurrentUserModel.UserAccountID);
            nannyViewModel.Item = nanny;
            NannyViewModelConverter.ConvertForEdit(nannyViewModel, nanny);

            return View(nannyViewModel);
        }

        [HttpPost]
        [ActionName(ActionConst.INFO)]
        public JsonResult UpdateNannyInfo(NannyViewModel nannyViewModel)
        {
            NannyViewModelConverter.ConvertForEdit(nannyViewModel.Item, nannyViewModel);
            BoolGenericResult<Nanny> result = this.AppService.Update(nannyViewModel.Item);

            BoolResult jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult);
        }

        [ActionName(ActionConst.AVATAR)]
        [HttpGet]
        public ActionResult GetNannyAvatar()
        {
            INannyAvatarViewModel model = ModelFactory.CreateModel<INannyAvatarViewModel>();
            Nanny nanny = NannyFactory.CreateNanny(this.Model.MasterModel.CurrentUserModel.UserAccountID);
            model.Avatar = this.AttachmentAppService.GetNannyAvatar(nanny);
            model.AttachmentGroup = AttachmentIdentifier.AttachmentGroup.NannyAvatar;
            if (this.HttpContext.Request["img"] != null)
            {
                model.AvatarForEdit = this.HttpContext.Request["img"].ToString();
            }
            else
            {
                model.AvatarForEdit = Project.Settings.ImageServer + Project.Settings.DefaultAvatarForEditUrl;
            }
            return View(model);
        }

        [ActionName(ActionConst.AVATAR)]
        [HttpPost]
        public JsonResult UpdateNannyAvatar(NannyAvatarViewModel nannyAvatar)
        {
            string temp = nannyAvatar.Avatar.Replace(Global.Settings.TempImageServer, "").Replace("%C3%97", "×");
            string directory = temp.Substring(0, temp.LastIndexOf('/') + 1);
            string oldFileName = temp.Substring(directory.LastIndexOf('/') + 1);
            string newFileName = "avatar" + oldFileName;
            FileUploadAppService.CutPhoto(AttachmentIdentifier.AttachmentGroup.NannyAvatar.GetHashCode(), directory, oldFileName, newFileName, nannyAvatar.Width, nannyAvatar.Height, nannyAvatar.X, nannyAvatar.Y);
            FileUploadAppService.CopyFile(directory, newFileName);

            Nanny nanny = this.AppService.GetById(this.Model.MasterModel.CurrentUserModel.UserAccountID);
            Attachment avatar = this.AttachmentAppService.GetNannyAvatar(nanny.ID);
            nanny.Attachments = new List<Attachment>();
            if (null != avatar)
            {
                nanny.Attachments.Add(avatar);
            }
            BoolGenericResult<Attachment> result = this.AppService.UpdateNannyAvatar(nanny, directory, newFileName);

            BoolResult jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult);
        }

        [ActionName(ActionConst.PHOTO)]
        [HttpGet]
        public ActionResult GetNannyPhoto()
        {
            IList<Attachment> photos = this.AttachmentAppService.GetNannyPhotos(this.Model.MasterModel.CurrentUserModel.UserAccountID);
            INannyPhotosViewModel nannyPhotosViewModel = ModelFactory.CreateModel<INannyPhotosViewModel>();
            nannyPhotosViewModel.AttachmentGroup = AttachmentIdentifier.AttachmentGroup.NannyPhoto;
            nannyPhotosViewModel.RestrictFileCount = this.AttachmentAppService.GetAttachmentGroupByID(AttachmentIdentifier.AttachmentGroup.NannyPhoto.GetHashCode()).RestrictFileCount;
            foreach (Attachment photo in photos)
            {
                INannyPhotoViewModel nannyPhotoViewModel = ModelFactory.CreateModel<INannyPhotoViewModel>();
                nannyPhotoViewModel.Photo = Project.Settings.ImageServer + photo.FileUrl + photo.FileName;
                nannyPhotoViewModel.PhotoID = photo.ID;
                nannyPhotosViewModel.Photos.Add(nannyPhotoViewModel);
            }

            return View(nannyPhotosViewModel);
        }

        [ActionName(ActionConst.PHOTO)]
        [HttpPost]
        public JsonResult UpdateNannyPhoto(NannyPhotoViewModel nannyPhoto)
        {
            string temp = nannyPhoto.Photo.Replace(Global.Settings.TempImageServer, "").Replace("%C3%97", "×"); ;
            string fileUrl = temp.Substring(0, temp.LastIndexOf('/') + 1);
            string fileName = temp.Substring(fileUrl.LastIndexOf('/') + 1).Replace("_360×360.jpg", "");
            FileUploadAppService.CopyFile(fileUrl, fileName);

            Nanny nanny = this.AppService.GetById(this.Model.MasterModel.CurrentUserModel.UserAccountID);
            IList<Attachment> photos = this.AttachmentAppService.GetNannyPhotos(nanny.ID);
            nanny.Attachments = new List<Attachment>();
            if (null != photos && photos.Count > 0)
            {
                nanny.Attachments = photos;
            }
            BoolGenericResult<Attachment> result = this.AppService.UpdateNannyPhoto(nanny, nannyPhoto.PhotoID, fileUrl, fileName);

            BoolResult jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult);
        }

        [ActionName(ActionConst.BIDCONSIGN)]
        [ChildActionOnly]
        public JsonResult AddConsignBidder(int id)
        {
            Consign consign = ConsignFactory.CreateConsign(id);
            Nanny nanny = NannyFactory.CreateNanny(this.Model.MasterModel.CurrentUserModel.UserAccountID);
            BoolGenericResult<ConsignBidder> result = this.ConsignAppService.AddConsignBidder(consign, nanny);

            BoolResult jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult);
        }

        [ActionName(ActionConst.BIDDINGCONSIGN)]
        public ViewResult GetBiddingConsigns()
        {
            INannyBiddingConsignViewModel nannyBiddingConsignViewModel = ModelFactory.CreateModel<INannyBiddingConsignViewModel>();
            IList<ConsignBidder> consignBidders = this.ConsignAppService.GetConsignsByNanny(this.Model.MasterModel.CurrentUserModel.UserAccountID);
            nannyBiddingConsignViewModel.ConsignBidders = ConsignBidderViewModelConverter.Convert(consignBidders);

            return View(nannyBiddingConsignViewModel);
        }

        [ActionName(ActionConst.UNBIDCONSIGN)]
        [ChildActionOnly]
        public JsonResult DeleteConsignBidder(int id)
        {
            Consign consign = ConsignFactory.CreateConsign(id);
            Nanny nanny = NannyFactory.CreateNanny(id);
            BoolGenericResult<ConsignBidder> result = this.ConsignAppService.UpdateConsignBidderStatusToCancel(consign, nanny, true);

            BoolResult jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult);
        }

        [ActionName(ActionConst.RESUMECONSIGN)]
        [ChildActionOnly]
        public JsonResult UpdateConsignBidder(int id)
        {
            Consign consign = ConsignFactory.CreateConsign(id);
            Nanny nanny = NannyFactory.CreateNanny(id);
            BoolGenericResult<ConsignBidder> result = this.ConsignAppService.UpdateConsignBidderStatusToCancel(consign, nanny, false);

            BoolResult jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult);
        }

        [ActionName(ActionConst.SETWORK)]
        [ChildActionOnly]
        public JsonResult UpdateWorking()
        {
            BoolGenericResult<Nanny> result = this.AppService.UpdateNannyWorkingStatus(NannyFactory.CreateNanny(this.Model.MasterModel.CurrentUserModel.UserAccountID), true);

            BoolResult jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult);
        }

        [ActionName(ActionConst.SETNOWORK)]
        [ChildActionOnly]
        public JsonResult UpdateNoWork()
        {
            BoolGenericResult<Nanny> result = this.AppService.UpdateNannyWorkingStatus(NannyFactory.CreateNanny(this.Model.MasterModel.CurrentUserModel.UserAccountID), false);

            BoolResult jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult);
        }

        [ActionName(ActionConst.REFRESH)]
        [ChildActionOnly]
        public JsonResult UpdateRefreshTime()
        {
            BoolGenericResult<NannyStatus> result = this.AppService.UpdateNannyRefreshTime(NannyFactory.CreateNanny(this.Model.MasterModel.CurrentUserModel.UserAccountID));

            BoolResult jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult);
        }

        [ActionName(ActionConst.LEAVEMESSAGE)]
        public JsonResult AddMessage(UserMessageViewModel userMessageViewModel)
        {
            userMessageViewModel.Item.MessagerUser.ID = this.Model.MasterModel.CurrentUserModel.UserAccountID;
            userMessageViewModel.Item.MessagerUser.Roles.Add(RoleFactory.CreateRole(SecurityIdentifier.Role.Nanny.GetHashCode()));
            BoolGenericResult<UserMessage> result = this.UserMessageAppService.Add(userMessageViewModel.Item);

            BoolResult jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult);
        }

        [ActionName(ActionConst.REPLY)]
        public JsonResult AddMessageReply(UserMessageViewModel userMessageViewModel)
        {
            userMessageViewModel.Item.MessagerUser.ID = this.Model.MasterModel.CurrentUserModel.UserAccountID;
            userMessageViewModel.Item.MessagerUser.Roles.Add(RoleFactory.CreateRole(SecurityIdentifier.Role.Nanny.GetHashCode()));
            BoolGenericResult<UserMessage> result = this.UserMessageAppService.AddUserMessageReply(userMessageViewModel.Item);

            BoolResult jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult);
        }

        [HttpGet]
        [ActionName(ActionConst.DIARY)]
        public ActionResult AddDiary(int? id)
        {
            NannyDiary nannyDiary = new NannyDiary();
            if (id.HasValue)
            {
                nannyDiary = this.NannyDiaryAppService.GetById(id.Value);
            }

            return View(nannyDiary);
        }

        [HttpPost]
        [ActionName(ActionConst.DIARY)]
        public ActionResult AddDiary(NannyDiaryViewModel nannyDiaryViewModel)
        {
            BoolGenericResult<NannyDiary> result = null;
            if (nannyDiaryViewModel.Item.ID > 0)
            {
                result = this.NannyDiaryAppService.Update(nannyDiaryViewModel.Item);
            }
            else
            {
                result = this.NannyDiaryAppService.Add(nannyDiaryViewModel.Item);
            }

            BoolResult jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return RedirectToAction("diary", new { id = nannyDiaryViewModel.Item.ID });
        }

        [ActionName(ActionConst.DIARIES)]
        public ActionResult GetDiaries(int id)
        {
            IDictionary<string, bool> orderBy = new Dictionary<string, bool>();
            orderBy.Add("CreateTime", true);
            NannyDiarySearch nannyDiarySearch = NannyDiarySearchFactory.CreateNannyDiarySearch(this.Model.MasterModel.CurrentUserModel.UserAccountID, orderBy, id, Project.Settings.MaxRowsPerPage);
            IList<NannyDiary> diaries = this.NannyDiaryAppService.GetBySearch(nannyDiarySearch).ToList();
            INannyDiaryViewModel nannyDiaryViewModel = ModelFactory.CreateModel<INannyDiaryViewModel>();
            nannyDiaryViewModel.Diaries = diaries;

            return View(nannyDiaryViewModel);
        }
    }
}
